package com.zjw.algorithm.ready1;

import com.zjw.algorithm.utils.ListNode;
import com.zjw.algorithm.utils.Utils;

public class _删除链表的中间节点 {

    public static void main(String[] args) {
        System.out.println(Utils.toString(new _删除链表的中间节点().deleteMiddle(Utils.asNodeList(new int[]{1}))));
    }

    public ListNode deleteMiddle(ListNode head) {

        if (head == null || head.next == null) {
            return null;
        }

        ListNode pFast = head;
        ListNode pSlow = head;
        ListNode pSlowPre = null;
        while (pFast != null && pFast.next != null) {
            pFast = pFast.next.next;
            pSlowPre = pSlow;
            pSlow = pSlow.next;
        }

        pSlowPre.next = pSlow == null ? null : pSlow.next;

        return head;
    }
}
